/**
 * Created by 小敏哥 on 2018/1/18.
 */
//获取应用实例
var app = getApp();

Page({
    data: {
        active: -1,
        isSend:true,
        addressList: [
            /*            {
                        "id": "F39EF840426146FFA78B5CD90EAD1C79",
                        "accountId": "EBB7911137864C3589C6B5F0D81DD4D0",
                        "provinceId": 440000,
                        "provinceName": "广东省",
                        "cityId": 441800,
                        "cityName": "清远市",
                        "countyId": 441882,
                        "countyName": "连州市",
                        "street": "具体来讲看看咯",
                        "postCode": null,
                        "contactName": "就可以",
                        "cellNum": "18075464646",
                        "defaultFlag": 1,
                        "fullAddr": "广东省清远市连州市具体来讲看看咯",
                        "source": 0,//0:地址添加 1：邮筒号添加
                        "mailboxNum": '123',
                        "mailboxNumRule": {
                            "showSendMailboxNum": 1,//寄件人，1：显示邮筒号，0显示地址
                            "showCollectMailboxNum": 0,//收件人
                        }

                    }*/
        ]
    },
    toAddAddress() {
        app.navigateTo({url: '../addAddress/addAddress'});
    },
    toAddMailbox() {
        app.navigateTo({url: '../addMailboxNum/addMailboxNum'});
    },
    onLoad(options) {
        //区分收件人地址与寄件人地址
        if(app.getStorageSync('addressType') != 'send') {
            wx.setNavigationBarTitle({
                title: '收件人地址管理',
            });
            this.setData({
                isSend:false,
            });
        }
    },

    onShow() {
        //首页会传回当前的选中项，用于初始化当前选中项，地址管理界面为无状态管理
        let selectedAddress = app.getStorageSync('selectedAddress');
        if (selectedAddress) {
            app.setStorageSync('currentEMSAddress', selectedAddress);
        }
        else{
            app.removeStorageSync('currentEMSAddress');
        }
        this.getAddressList();
    },
    //设置默认项
    setActive(e) {
        this.setData({
            active: e.detail.active,
        });
    },

    //选择地址项
    chooseAddressItem(e) {
        app.setStorageSync('currentEMSAddress', this.data.addressList[e.detail.active]);
        //  app.globalData.isHoldAddress = 1  //标识进入页面有点击地址列表
        //  app.setStorageSync('sendSign',0) //如果寄件有选择过地址，就取选择的地址，不取默认地址
        wx.navigateBack();
    },

    deleteCallBack(e) {//子组件删除地址后的回调
        //获取当前缓存的选中项
        let currentAddress = app.getStorageSync('currentEMSAddress');
        //删除项为当前缓存选中项时将缓存删除
        if (e.detail.addressId == currentAddress.id) {
            //app.globalData.isHoldAddress = 2  //标识删除选择的地址
            app.removeStorageSync('currentEMSAddress');
        }
    },

    getAddressList() {
        //区分收件人地址和寄件人地址
        let apiData=app.getStorageSync('addressType') == 'send'?{}:{collectFlag:1};
        let opt = {
            url: app.globalData.host + "/wx/ad/getList.ajax",
            data: apiData,
            sucFn: (result) => {
                if (result.code == 1000) {
                    let defaultActive = -1;
                    for (let i = 0; i < result.body.addressMsgList.length; i++) {
                        if (result.body.addressMsgList[i].defaultFlag == 1) {
                            defaultActive = i;
                            break;
                        }
                    }
                    this.setData({
                        addressList: result.body.addressMsgList,
                        active: defaultActive,
                    })
                }
                else {
                    wx.showToast({
                        title: result.msg,
                        icon: 'none',
                        duration: 2000
                    });
                }
            }
        };
        app.ajax(opt);
    },

});